package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.dao.AnalyzeObject;
import com.greenline.search.test.utils.FileUtils;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-23
 * Time: 下午3:49
 * To change this template use File | Settings | File Templates.
 */
public class RecognitionAndUUID {
    private static Logger logger = LoggerFactory.getLogger(RecognitionAndUUID.class);
    private static String recURL = "http://192.168.1.37:8098/analyze/?q=";
    private static String uuidURL = "http://10.10.100.153:8090/analyze/?q=";

    public static void main(String[] args) throws IOException {
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/user_keyword_20140115.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath,"GBK");
        long startTime = System.currentTimeMillis();
        StringBuffer sb = new StringBuffer();
        StringBuffer sb2 = new StringBuffer();
        int count = 0;
        for(String keyword : keywordList){
            if(count++ % 1000 == 0){
                System.out.println("Deal with " + count + " keywords already.");
            }
            boolean flag = false;
            String uuidFullURL = uuidURL + URLEncoder.encode(keyword,"UTF-8");
            String recFullURL = recURL + URLEncoder.encode(keyword,"UTF-8");

            List<AnalyzeObject> objectList = JSONStreamParserForAnalyze.parse(uuidFullURL);
            if(objectList.size() == 1){
                String uuid = objectList.get(0).getUuid();
                String type = objectList.get(0).getType();
                if(uuid != null && !uuid.isEmpty()){
                    List<AnalyzeObject> recResult = JSONStreamParserForAnalyze.parse(recFullURL);
                    if(recResult.size() == 1){
                        String recType = recResult.get(0).getType().replace("_class","");
                        if(recType.equals(type)){
                            flag = true;
                        }
                        if(!flag){
                            sb.append(keyword).append("\t").append(type).append("\t").append(recType).append("\n");
                        }
                    }else {
                        sb2.append(keyword).append("\t").append(type).append("\t");
                        for(AnalyzeObject object : recResult){
                            sb2.append(object.getKey()).append(" ").append(object.getType()).append("\t");
                        }
                        sb2.append("\n");
                    }
                }
            }
        }
        logger.info(sb.toString());
        logger.info(sb2.toString());
        System.out.println("test end...");
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime/1000) + " seconds.");
    }
}
